using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Acms.AcmsControls
{
    public abstract class RenderSearchEngineSiteMap : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //Get the site from the request name
            string siteRoot = Request.Url.DnsSafeHost;
            if (Request.ApplicationPath != "/")
                siteRoot = string.Concat(siteRoot, Request.ApplicationPath);
            Bll.Site site = Bll.Site.GetByUrl(siteRoot);
            Bll.Menu menu = null;
            if (site == null)
            {
                throw new Acms.Exceptions.PostNotFoundException(string.Concat("Could not load site from ", siteRoot));
            }
            else
            {
                string menuName = Request.QueryString["Menu"];
                if (!string.IsNullOrEmpty(menuName))
                {
                    menu = Acms.Bll.Menu.GetByNameAndSiteId(menuName, site.Id);
                    if (menu == null)
                    {
                        throw new Acms.Exceptions.PostNotFoundException(string.Concat("Could not load menu with name ", menuName));
                    }
                }
                else
                {
                    List<Acms.Bll.Menu> menus = Acms.Bll.Menu.GetAllBySiteId(site.Id);
                    if (menus != null && menus.Count > 0)
                    {
                        menu = menus[0];
                    }
                    if (menu == null)
                    {
                        throw new Acms.Exceptions.PostNotFoundException("Could not load menu");
                    }
                }
            }


            Response.ContentType = "text/xml";
            XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xml.WriteStartDocument();
            xml.WriteStartElement("urlset");
            xml.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"); 

            List<Acms.Bll.Post> posts = null;
            if (string.IsNullOrEmpty(menu.ParentPath))
            {
                posts = new List<Acms.Bll.Post>();
                posts.Add(Acms.Bll.Post.GetTopLevelBySiteIdAndMenuId(site.Id, menu.Id));
            }
            else if(menu.IncludeParent && menu.ParentId != 0)
            {
                posts = new List<Acms.Bll.Post>();
                posts.Add(Acms.Bll.Post.GetById(menu.ParentId));
            }
            else
            {
                posts = Acms.Bll.Post.GetAllByMenuIdAndParentId(menu.Id, menu.ParentId);
            }

            WriteNodes(posts, xml, site.Id, menu.Id, siteRoot);

            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Close();
            Response.Flush();
            Response.End();
        }

        private void WriteNodes(List<Acms.Bll.Post> posts, XmlTextWriter xml, int siteId, int menuId, string root)
        {
            if (posts != null)
            {
                foreach (Acms.Bll.Post p in posts)
                {
                    if(p != null)
                        WriteNode(p, xml, root);
                }
                foreach (Acms.Bll.Post p in posts)
                {
                    if (p != null)
                        WriteNodes(Acms.Bll.Post.GetAllByMenuIdAndParentId(menuId, p.Id), xml, siteId, menuId, root);
                }
            }
        }

        private void WriteNode(Acms.Bll.Post p, XmlTextWriter xml, string root)
        {
            xml.WriteStartElement("url");

            xml.WriteStartElement("loc");

            xml.WriteRaw(string.Concat("http://", p.Url.Replace("~", root)));

            xml.WriteEndElement();

            xml.WriteEndElement();
        }


    }
}
